<?php

declare(strict_types=1);

namespace app\controller\api;

use app\model\AdminUser;
use app\util\Sms;
use app\util\Tools;
use think\facade\Db;
use think\Response;
use app\util\WechatService;
use app\model\AdminUserData;

/**
 * 驾校平台端用户管理
 */
class JxAdminUser extends Base
{
    /**
     *
     *用户列表
     */
    public function usersList(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $where['status'] = array('in', '1,2');
        if (isset($param['phone']) && $param['phone'] != '') {
            $where[] = ['user_phone', 'like', '%' . $param['phone'] . '%'];
        }
        $field = 'id,user_img,nickname,user_phone,add_time,user_name';
        $list = Db::table('jx_users')->page((int)$page, (int)$limit)->where($where)->field($field)->order('id desc')->select();
        $count = Db::table('jx_users')->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count;
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }


    /**
     *
     *获取用户信息
     */
    public function getAdminUserInfo(): Response
    {
        $postData = $this->request->post();
        $id = $postData['id'];//用户id
        $res =Db::table('jx_users')->where('id',$id)->find();
        if ($res === false) {
            return $this->buildFailed(402, '修改失败');
        }
        return $this->buildSuccess($res);
    }

    //设为代理

    public function setAgent(): Response
    {
        $param = $this->request->post();
        $id = $param['id'];//用户id
        $is_agent = isset($param['is_agent']) && ($param['is_agent'] != '') ? $param['is_agent'] : 1;  //1设为代理 ，2取消代理
        $res =Db::table('jx_users')->where('id',$id)->save(['is_agent'=>$is_agent]);
        return $this->buildSuccess();
    }

    //冻结或者注销用户
    public function setUserStatus(): Response
    {
        $param = $this->request->post();
        $id = $param['id'];//用户id
        $status = isset($param['status']) && ($param['status'] != '') ? $param['status'] : 1;  //2冻结用户 3注销用户 1正常
        $res =Db::table('jx_users')->where('id',$id)->save(['status'=>$status]);
        return $this->buildSuccess();
    }
    
    //解冻用户
    public function thawUserStatus():Response{
        $param = $this->request->post();
        $id = $param['id'];//用户id
        $status = isset($param['status']) && ($param['status'] != '') ? $param['status'] : 1;  
        $map['update_time'] = date('Y-m-d H:i:s',time());
        $map['status'] = $status;
        $res =Db::table('jx_users')->where('id',$id)->save($map);

        return $this->buildSuccess();
    }


    //获取消费订单

    public function orderList(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, '用户id 参数错误');
        }

        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $where['status'] = array('in', '1,2');
        // if (isset($param['car_brand_name']) && $param['car_brand_name'] != '') {
        //     $where[] = ['car_brand_name', 'like', '%' . $param['car_brand_name'] . '%'];
        // }
        $field = 'order_sn,service_type,add_time,actual_paid';
        $where['order_status'] = 1;//已付款
        $where['user_id'] = $param['id'];
        $list = Db::table('jx_orders')->page((int)$page, (int)$limit)->field($field)->where($where)->order('id desc')->select();
        $count = Db::table('jx_orders')->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count;
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    /**
     *  获取该用户返佣记录
     */ 
    public function getUserBrokerageChange(){
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        if (isset($param['user_id']) && $param['user_id'] == '') {
            return $this->buildFailed(400, '用户id 参数错误');
        }

        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
  
        $field = 'u.from_user_id,u.change_title,change_class,change_price,us.user_name,us.user_phone,us.sex,c.class_name,ud.is_build';
        $where['u.user_id'] = $param['user_id'];
        $where['change_class'] = '2'; //返佣
        $where['u.status'] = array('in', '1,2');

        $list = Db::table('jx_user_brokerage_change')->alias('u')
        ->join('jx_user_dossier ud','ud.user_id = u.from_user_id')
        ->join('jx_class c','c.id = ud.class_id')
        ->join('jx_users us','us.id = ud.user_id')
        ->page((int)$page, (int)$limit)->field($field)->where($where)->order('u.id desc')->select();
        $count = Db::table('jx_user_brokerage_change')->alias('u')
        ->join('jx_user_dossier ud','ud.user_id = u.from_user_id')
        ->join('jx_class c','c.id = ud.class_id')
        ->join('jx_users us','us.id = ud.user_id')
        ->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count; 
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }
  //获取返佣用户的详细信息
    public function getReturnUserInfo(){
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        if (isset($param['from_user_id']) && $param['from_user_id'] == '') {
            return $this->buildFailed(400, '用户id 参数错误');
        }
        $where['ud.user_id'] = $param['from_user_id'];
        $field = 'ud.*,c.class_name,s.shcool_name,st.state_name,coach.user_name as coach_user_name';
        $res = Db::table('jx_user_dossier')->alias('ud')
        ->join('jx_class c','c.id = ud.class_id')
        ->join('jx_users us','us.id = ud.user_id')
        ->join('jx_school s','s.id = ud.school_id')
        ->join('jx_school_state st','st.id = ud.state_id')
        ->leftjoin('jx_coach coach','coach.id = ud.coach_id')
        ->field($field)->where($where)->find();

        if ($res) {
            return $this->buildSuccess($res);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }

    }

    /**
     *  获取用户余额明细
     */ 
    public function getUserBalanceChange(){
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        if (isset($param['user_id']) && $param['user_id'] == '') {
            return $this->buildFailed(400, '用户id 参数错误');
        }
        //获取该用户的余额总收益和日收益
        $userInfo = Db::table('jx_users')->where('id',$param['user_id'])->find();
        $balance = $userInfo['balance'];
        //获取收益
        $sumIncome = Db::table('jx_user_balance_change')->where('change_type','2')->sum('change_price');
        //获取今日收益
        $todayIncome =  Db::table('jx_user_balance_change')->where('change_type','2')->whereDay('add_time')->sum('change_price');
        $data['user_ifno']['balance'] = $balance;
        $data['user_ifno']['sumIncome'] = $sumIncome;
        $data['user_ifno']['todayIncome'] = $todayIncome;

        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $field = 'id,change_type,change_title,add_time,change_price';
        $where['user_id'] = $param['user_id'];
        $where['status'] = array('in', '1,2');

        $list = Db::table('jx_user_balance_change')
        ->page((int)$page, (int)$limit)->field($field)->where($where)->order('id desc')->select();
        $count = Db::table('jx_user_balance_change')
        ->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count; 
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }

    }


}